<template>
  <div class="calendar">
    <div class="header">
      <button @click="prevMonth">上月</button>
      <h2>{{ year }}年 {{ month + 1 }}月</h2>
      <button @click="nextMonth">下月</button>
    </div>
    <div class="weekdays">
      <span>日</span>
      <span>一</span>
      <span>二</span>
      <span>三</span>
      <span>四</span>
      <span>五</span>
      <span>六</span>
    </div>
    <div class="days">
      <span
        v-for="day in daysInMonth"
        :key="day"
        :class="{ 'current-day': isCurrentDay(year, month, day) }"
        @click="showReminderModal(day)"
      >
        {{ day }}
        <span v-if="hasReminder(year, month, day)" class="reminder-dot"></span>
      </span>
    </div>
    <!-- 提醒模态框 -->
    <div v-if="showModal" class="modal">
      <div class="modal-content">
        <span class="close" @click="showModal = false">&times;</span>
        <h2>{{ selectedDate }} 提醒设置</h2>
        <input v-model="reminderTime" type="datetime-local" />
        <input v-model="reminderText" type="text" placeholder="提醒内容" />
        <button @click="addReminder">添加提醒</button>
        <ul>
          <li v-for="(reminder, index) in getReminders(selectedDate)" :key="index">
            {{ reminder.time }} - {{ reminder.text }}
            <button @click="deleteReminder(selectedDate, index)">删除</button>
          </li>
        </ul>
      </div>
    </div>
  </div>
</template>

<script setup>
  import { ref, computed, onMounted } from 'vue'

  // 当前日期
  const currentDate = ref(new Date())
  const year = ref(currentDate.value.getFullYear())
  const month = ref(currentDate.value.getMonth())

  // 提醒数据
  const reminders = ref({})

  // 模态框状态
  const showModal = ref(false)
  const selectedDate = ref('')
  const reminderTime = ref('')
  const reminderText = ref('')

  // 获取指定月份的天数
  const daysInMonth = computed(() => {
    const days = []
    const firstDay = new Date(year.value, month.value, 1).getDay()
    const lastDay = new Date(year.value, month.value + 1, 0).getDate()

    for (let i = 1; i <= lastDay; i++) {
      days.push(i)
    }

    return days
  })

  // 切换到上一个月
  const prevMonth = () => {
    if (month.value === 0) {
      month.value = 11
      year.value--
    } else {
      month.value--
    }
  }

  // 切换到下一个月
  const nextMonth = () => {
    if (month.value === 11) {
      month.value = 0
      year.value++
    } else {
      month.value++
    }
  }

  // 判断是否为当前日期
  const isCurrentDay = (y, m, d) => {
    return (
      y === currentDate.value.getFullYear() &&
      m === currentDate.value.getMonth() &&
      d === currentDate.value.getDate()
    )
  }

  // 判断某一天是否有提醒
  const hasReminder = (y, m, d) => {
    const dateKey = `${y}-${m + 1}-${d}`
    return reminders.value[dateKey] && reminders.value[dateKey].length > 0
  }

  // 显示提醒模态框
  const showReminderModal = (day) => {
    selectedDate.value = `${year.value}-${month.value + 1}-${day}`
    showModal.value = true
  }

  // 添加提醒
  const addReminder = () => {
    if (reminderTime.value && reminderText.value) {
      const dateKey = selectedDate.value
      if (!reminders.value[dateKey]) {
        reminders.value[dateKey] = []
      }
      reminders.value[dateKey].push({
        time: reminderTime.value,
        text: reminderText.value
      })
      reminderTime.value = ''
      reminderText.value = ''
      showModal.value = false
    }
  }

  // 删除提醒
  const deleteReminder = (date, index) => {
    if (reminders.value[date]) {
      reminders.value[date].splice(index, 1)
    }
  }

  // 获取某一天的提醒列表
  const getReminders = (date) => {
    return reminders.value[date] || []
  }

  onMounted(() => {
    // 初始化日历
  })
</script>

<style scoped>
  .calendar {
    width: 300px;
    border: 1px solid #ccc;
    padding: 10px;
    border-radius: 5px;
    box-shadow: 0 0 5px rgba(0, 0, 0, 0.1);
  }

  .header {
    display: flex;
    justify-content: space-between;
    align-items: center;
    margin-bottom: 10px;
  }

  .weekdays {
    display: flex;
    justify-content: space-around;
    margin-bottom: 10px;
  }

  .days {
    display: grid;
    grid-template-columns: repeat(7, 1fr);
    gap: 5px;
  }

  .days span {
    text-align: center;
    padding: 5px;
    cursor: pointer;
  }

  .current-day {
    background-color: #e0f7fa;
    border-radius: 50%;
  }

  .reminder-dot {
    display: block;
    width: 5px;
    height: 5px;
    background-color: red;
    border-radius: 50%;
    margin: 2px auto 0;
  }

  .modal {
    position: fixed;
    top: 0;
    left: 0;
    width: 100%;
    height: 100%;
    background-color: rgba(0, 0, 0, 0.5);
    display: flex;
    justify-content: center;
    align-items: center;
  }

  .modal-content {
    background-color: white;
    padding: 20px;
    border-radius: 5px;
    box-shadow: 0 0 5px rgba(0, 0, 0, 0.1);
    position: relative;
  }

  .close {
    position: absolute;
    top: 10px;
    right: 10px;
    font-size: 20px;
    cursor: pointer;
  }
</style>
